/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Julian
 */
public class DBhelper extends ReplyDAO{

    @Override
    public String getReply(String keyw, String cat) {
        
        try {
            // TODO code application logic here
            DBConnectionFactory myFactory = DBConnectionFactory.getInstance(DAOFactory.MYSQL);
            Connection conn = myFactory.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("select answer from word where category = ? and keyword like ?");
            //System.err.println("INSIDE DBHELPER KEYWORD = "+keyw+"& CATEGORY = "+cat);
            pstmt.setString(1, cat);
            pstmt.setString(2, "%"+keyw+"%");
            ResultSet rs=pstmt.executeQuery();
            String reply = "null";
            
            while(rs.next())
            {                 
                reply = rs.getString("answer");
            }
            conn.close();
            return reply;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return "null";
        
    }

    @Override
    public int addWord(Word w) {
               
           try {
            // TODO code application logic here
            DBConnectionFactory myFactory = DBConnectionFactory.getInstance(DAOFactory.MYSQL);
            Connection conn = myFactory.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("Insert into word(category,answer,keyword)VALUES(?,?,?) ");
            pstmt.setString(1, w.getCategory());
            pstmt.setString(2, w.getAnswer());
            pstmt.setString(3, w.getKeyword());
            pstmt.executeUpdate();
            
       }catch (SQLException ex) {
            ex.printStackTrace();
       }
       return 0;
    }
 
}
